package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.BuryingDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.BuryingDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/08/19 18:00
 */
@Service("buryingDAO")
public class MongoBuryingDAO extends AbstractBaseDAO<BuryingDO> implements BuryingDAO {

    @Override
    public void insert(BuryingDO burying) {
        super.insert(burying);
        mongoTemplate.insert(burying, COLLECTION_NAME);
    }

    @Override
    public void update(BuryingDO burying) {
        super.update(burying);
        mongoTemplate.save(burying, COLLECTION_NAME);
    }

    @Override
    public List<BuryingDO> queryAll() {
        Criteria criteria = Criteria.where(BuryingColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public PageList<BuryingDO> pageQuery(BuryingDO burying, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(BuryingColumn.ENV).is(DEFAULT_ENV);
        // 查询参数
        if (StringUtils.isNotBlank(burying.getSystemKey())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(BuryingColumn.SYSTEM_KEY).regex("^.*" + burying.getSystemKey() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(burying.getUseCase())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(BuryingColumn.USE_CASE).regex("^.*" + burying.getUseCase() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(burying.getModule())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(BuryingColumn.MODULE).regex("^.*" + burying.getModule() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(burying.getCategory())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(BuryingColumn.CATEGORY).regex("^.*" + burying.getCategory() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(burying.getSubCategory())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(BuryingColumn.SUB_CATEGORY).regex("^.*" + burying.getSubCategory() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(burying.getTraceId())) {
            criteria.and(BuryingColumn.TRACE_ID).is(burying.getTraceId());
        }
        if (StringUtils.isNotBlank(burying.getBusinessNo())) {
            criteria.and(BuryingColumn.BUSINESS_NO).is(burying.getBusinessNo());
        }
        if (StringUtils.isNotBlank(burying.getLoggerLevel())) {
            criteria.and(BuryingColumn.LOGGER_LEVEL).is(burying.getLoggerLevel());
        }
        if (StringUtils.isNotBlank(burying.getUniqueKey())) {
            criteria.and(BuryingColumn.UNIQUE_KEY).is(burying.getUniqueKey());
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, BuryingColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public BuryingDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(BuryingColumn._ID).is(_id);
        criteria.and(BuryingColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return BuryingColumn.class;
    }

    @Override
    public void batchInsert(List<BuryingDO> buryingList) {
        super.batchInsert(buryingList);
        mongoTemplate.insert(buryingList, COLLECTION_NAME);
    }

    @Override
    public List<BuryingDO> queryBySystemAndUseCase(String systemUuid, String useCaseKey, String traceId, String orderSerialNo) {
        Criteria criteria = Criteria.where(BuryingColumn.ENV).is(DEFAULT_ENV);
        criteria.and(BuryingColumn.SYSTEM_KEY).is(systemUuid);
        criteria.and(BuryingColumn.USE_CASE).is(useCaseKey);
        if (StringUtils.isNotBlank(traceId)) {
            criteria.and(BuryingColumn.TRACE_ID).is(traceId);
        }
        if (StringUtils.isNotBlank(orderSerialNo)) {
            criteria.and(BuryingColumn.BUSINESS_NO).is(orderSerialNo);
        }
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    protected void doInsert(BuryingDO buryingDO) {
        super.doInsert(buryingDO);

        // TODO 判断逻辑设计
        // ----
        if (StringUtils.isBlank(buryingDO.getUniqueKey())) {
            String systemKey = StringUtils.defaultString(buryingDO.getSystemKey(), StringUtils.EMPTY);
            String useCase = StringUtils.defaultString(buryingDO.getUseCase(), StringUtils.EMPTY);
            String module = StringUtils.defaultString(buryingDO.getModule(), StringUtils.EMPTY);
            String category = StringUtils.defaultString(buryingDO.getCategory(), StringUtils.EMPTY);
            String subCategory = StringUtils.defaultString(buryingDO.getSubCategory(), StringUtils.EMPTY);
            List<String> keyList = Lists.newArrayList(systemKey, useCase, module, category, subCategory);
            buryingDO.setUniqueKey(StringUtils.join(keyList, "-"));
        }

        if (buryingDO.getTime() == 0) {
            buryingDO.setTime(System.currentTimeMillis());
        }
    }
}
